const express = require('express');
const router = express.Router();
const db = require('../utils/db')

//渲染
router.get('/schoolGet',(req,res)=>{
    console.log(req.query);
    const sql = `select * from school;`;
    // const sql2 = `select count(*) as total from school where s_name like '%${search}%';`;
    db.query(sql, (err, data) => {
        if (err) {
            res.send({ error: 1, msg: err })
        } else {
            console.log(data);
            if (data.length > 0) {
                res.send({ error: 0, data: data })

            } else {
                res.send({ error: 1, msg: err })
            }

        }
    })
})

//添加
router.post('/schoolAdd',(req,res)=>{
    const {s_name,s_code,s_area,s_transitiona,s_transname,s_frequency,s_collection,s_collecname,s_remittance,s_type,s_address,s_state} = req.body;
    const sql = `insert into school values(null,'${s_name}','${s_code}','${s_area}','${s_transitiona}','${s_transname}','${s_frequency}','${s_collection}','${s_collecname}','${s_remittance}','${s_type}','${s_address}','${s_state}');`;
    db.query(sql,(err,data)=>{
        if(err){
            res.json({error:1,msg:err});
        }else{
            res.json({error:0,msg:'添加成功'});
        }
    })
})

module.exports = router;